package cn.springbook.platform.test.util.generator;

import cn.springbook.common.core.domain.*;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Scanner;

// 演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入" + tip + "：");
        scanner.useDelimiter("\n");
        if (scanner.hasNext()) {
            String ipt = scanner.nextLine();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt.trim();
            } else {
                return null;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        DataSourceConfig dsc = new DataSourceConfig
                .Builder("jdbc:mysql://127.0.0.1:3306/sunshine_test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC&useSSL=false", "root", "root")
                .build();
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator(dsc);
        // 全局配置
        String moduleName = scanner("模块名(输入空格代表忽略)");
        // String projectPath = System.getProperty("user.dir");
        StringBuilder projectPath = new StringBuilder();
        projectPath.append(System.getProperty("user.dir"));
        projectPath.append("/sunshine-platform");
        projectPath.append("/sunshine-platform-test");
        projectPath.append("/src/main/java");


        // 全局配置
        GlobalConfig globalConfig = new GlobalConfig
                .Builder()
                .outputDir(projectPath.toString())
                .author("caifenglin")
                .openDir(false)
                // 实体属性 Swagger2 注解
                .enableSwagger()
                .fileOverride()
                .build();


        String subPackage = StringUtils.isNotBlank(moduleName) ? ("." + moduleName) : "";
        // 包配置
        PackageConfig packageConfig = new PackageConfig
                .Builder()
                .parent("cn.springbook.platform.test")
                .entity("entity" + subPackage)
                .mapper("mapper" + subPackage)
                .service("service" + subPackage)
                .serviceImpl("service.impl" + subPackage)
                .controller("controller" + subPackage)
                // .xml(null)
                .build();

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig
                .Builder()
                .mapperXml(null)
                .build();

        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig
                .Builder()
                .addInclude(scanner("表名，多个英文逗号分割").split(","))
                .addTablePrefix("t_", "sys_")
                .entityBuilder().naming(NamingStrategy.underline_to_camel)
                .addIgnoreColumns("")
                .addSuperEntityColumns("ID, CREATE_USER, CREATE_TIME, UPDATE_USER, UPDATE_TIME, VERSION, DELETED")
                .enableSerialVersionUID()
                .enableLombok()
                .entityBuilder().superClass(BaseEntity.class)
                .convertFileName((fileName -> String.format("%sEntity", fileName)))
                .serviceBuilder().superServiceClass(BaseService.class)
                .convertServiceFileName((fileName -> String.format("%sService", fileName)))
                .superServiceImplClass(BaseServiceImpl.class)
                .convertServiceImplFileName((fileName -> String.format("%sServiceImpl", fileName)))
                .mapperBuilder().superClass(BaseMapper.class)
                .convertMapperFileName((fileName -> String.format("%sMapper", fileName)))
                .convertXmlFileName((fileName -> String.format("%sMapper", fileName)))
                .controllerBuilder()
                .convertFileName((fileName -> String.format("%sController", fileName)))
                .superClass(BaseController.class)
                .build();

        mpg.global(globalConfig);
        mpg.packageInfo(packageConfig);
        mpg.template(templateConfig);
        mpg.strategy(strategyConfig);
        mpg.execute(new FreemarkerTemplateEngine());
    }

}